package com.liuboya.service.tablebiz.wbhotsearchrank.Impl;

import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.ReUtil;
import com.alibaba.fastjson.JSONObject;
import com.liuboya.dao.weibo.wbhotsearchrank.dao.WbHotSearchRankDao;
import com.liuboya.dao.weibo.wbhotsearchrank.entity.WbHotSearchRank;
import com.liuboya.dao.weibo.wbhotsearchrank.entity.WbHotSearchRankExample;
import com.liuboya.dao.weibo.wbhotsearchrank.entity.WbHotSearchRankExtra;
import com.liuboya.service.tablebiz.wbhotsearchrank.WbHotSearchRankService;
import com.liuboya.weibomodel.model.enums.DeleteEnum;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;
import java.util.Objects;

/**
 * 描述：
 *
 * @author yeyuan
 * @date 2021/08/13 15:45
 **/
@Service
public class WbHotSearchRankServiceImpl implements WbHotSearchRankService {
    @Resource
    private WbHotSearchRankDao wbhotsearchrankDao;

    @Override
    public Long insert(WbHotSearchRank wbhotsearchrank) {
        return wbhotsearchrankDao.insert(wbhotsearchrank);
    }

    @Override
    public List<WbHotSearchRank> find(WbHotSearchRankExample example) {
        return wbhotsearchrankDao.find(example);
    }

    @Override
    public Integer update(WbHotSearchRank wbhotsearchrank, WbHotSearchRankExample example) {
        return wbhotsearchrankDao.update(wbhotsearchrank, example);
    }

    @Override
    public Integer count(WbHotSearchRankExample example) {
        return wbhotsearchrankDao.count(example);
    }

    @Override
    public WbHotSearchRank findByUnique(Integer recordTime, String words) {
        if (Objects.isNull(recordTime) || StringUtils.isBlank(words)) {
            return null;
        }
        WbHotSearchRankExample example = new WbHotSearchRankExample();
        example.createCriteria().andRecordTimeEqualTo(recordTime)
                .andWordsEqualTo(words)
                .andIsDeletedEqualTo(DeleteEnum.DELETED_NOT.getVal());
        List<WbHotSearchRank> wbHotSearchRanks = this.find(example);
        return wbHotSearchRanks.isEmpty() ? null : wbHotSearchRanks.get(0);
    }

    @Override
    public WbHotSearchRank jsonToWbInsert(JSONObject jsonObject) {
        if (Objects.isNull(jsonObject)) {
            return null;
        }
        //当前小时整点时间
        DateTime dateTime = DateUtil.beginOfHour(new Date());
        int time = (int) (dateTime.getTime() / 1000);
        String desc = jsonObject.getString("desc");
        if (StringUtils.isBlank(desc)) {
            return null;
        }
        WbHotSearchRank byUnique = this.findByUnique(time, desc);
        if (Objects.nonNull(byUnique)) {
            return null;
        }
        String icon = jsonObject.getString("icon");
        String pic = jsonObject.getString("pic");
        //热度值
        String descExtr = jsonObject.getString("desc_extr");
        Integer hotValue = ReUtil.getFirstNumber(descExtr);
        WbHotSearchRank insert = new WbHotSearchRank();
        insert.setRecordTime(time);
        insert.setWords(desc);
        insert.setHeadIcon(pic);
        insert.setTailIcon(icon);
        insert.setHotValue(hotValue);
        //判断热搜中的置顶和商业推广
        if (Objects.isNull(descExtr)) {
            WbHotSearchRankExtra extra = new WbHotSearchRankExtra();
            String type = "";
            if (pic.contains("search_stick")) {
                type = "置顶";

            } else if (StringUtils.isNotBlank(icon) && icon.contains("_jian")) {
                type = "商业推广";
            }
            if (StringUtils.isNotBlank(type)) {
                extra.setType(type);
                insert.setExtra(JSONObject.toJSONString(extra));
            }
        }
        return insert;
    }

}